/*
 * Copyright (c) 2015, the Dart project authors.
 *
 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package vm.element

// This file is generated by the script: pkg/vm_service/tool/generate.dart in dart-lang/sdk.

import com.google.gson.JsonObject

/**
 * {@link ErrorObj} 表示 Dart 语言级别的错误。这与 RPC 错误不同。
 */
class ErrorObj(json: JsonObject) : Obj(json) {

    /**
     * 如果此错误是由于未处理的异常引起的，则这是抛出的异常。
     *
     * 可以返回 <code>null</code>。
     */
    fun getException(): InstanceRef? {
        val obj = json.get("exception")?.asJsonObject ?: return null
        return InstanceRef(obj)
    }

    /**
     * 这是什么类型的错误？
     */
    fun getKind(): ErrorKind {
        val value = json.get("kind")
        try {
            return if (value == null) ErrorKind.Unknown else ErrorKind.valueOf(value.asString)
        } catch (e: IllegalArgumentException) {
            return ErrorKind.Unknown
        }
    }

    /**
     * 发生的错误的描述。
     */
    fun getMessage(): String {
        return getAsString("message")!!
    }

    /**
     * 如果此错误是由于未处理的异常引起的，则这是堆栈跟踪对象。
     *
     * 可以返回 <code>null</code>。
     */
    fun getStacktrace(): InstanceRef? {
        val obj = json.get("stacktrace")?.asJsonObject ?: return null
        return InstanceRef(obj)
    }
}
